package net.osc.web;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

/**
 * 在线用户
 * 
 * @author <a href=mailto:peng.wu@foxmail.com>wu.peng</a> 2012-10-29
 * 
 */
public class OnlineUser implements HttpSessionBindingListener, Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final String SESSION_USER = "webapp_session_user";
	public static final String ONLINE_MAP = "webapp_online_map";
	public static final String SESSION_MAP = "webapp_session_map";

	private String userCode;
	private String userName;
	private String orgCode;
	private String orgName;
	private String sessionId;
	private Date loginTime;

	public OnlineUser() {

	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getOrgName() {
		return orgName;
	}

	public void setOrgName(String orgName) {
		this.orgName = orgName;
	}

	public String getUserCode() {
		return userCode;
	}

	public void setUserCode(String userCode) {
		this.userCode = userCode;
	}

	public String getOrgCode() {
		return orgCode;
	}

	public void setOrgCode(String orgCode) {
		this.orgCode = orgCode;
	}

	public String getSessionId() {
		return sessionId;
	}

	public void setSessionId(String sessionId) {
		this.sessionId = sessionId;
	}

	public Date getLoginTime() {
		return loginTime;
	}

	public void setLoginTime(Date loginTime) {
		this.loginTime = loginTime;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((sessionId == null) ? 0 : sessionId.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		OnlineUser other = (OnlineUser) obj;
		if (sessionId == null) {
			if (other.sessionId != null)
				return false;
		} else if (!sessionId.equals(other.sessionId))
			return false;
		return true;
	}

	public void valueBound(HttpSessionBindingEvent event) {
		Map<String, OnlineUser> onlineMap = OnlineUser.getOnlineMap(event
				.getSession().getServletContext());
		onlineMap.put(event.getSession().getId(), this);
		Map<String, HttpSession> sessionMap = OnlineUser.getSessionMap(event
				.getSession().getServletContext());
		sessionMap.put(event.getSession().getId(), event.getSession());

	}

	public void valueUnbound(HttpSessionBindingEvent event) {
		Map<String, OnlineUser> onlineMap = OnlineUser.getOnlineMap(event
				.getSession().getServletContext());
		onlineMap.remove(event.getSession().getId());
		Map<String, HttpSession> sessionMap = OnlineUser.getSessionMap(event
				.getSession().getServletContext());
		sessionMap.remove(event.getSession().getId());
	}

	@SuppressWarnings("unchecked")
	public static synchronized Map<String, OnlineUser> getOnlineMap(
			ServletContext context) {
		Map<String, OnlineUser> map = (Map<String, OnlineUser>) context
				.getAttribute(ONLINE_MAP);
		if (map == null) {
			map = new ConcurrentHashMap<String, OnlineUser>();
			context.setAttribute(ONLINE_MAP, map);
		}
		return map;
	}

	@SuppressWarnings("unchecked")
	public static synchronized Map<String, HttpSession> getSessionMap(
			ServletContext context) {
		Map<String, HttpSession> map = (Map<String, HttpSession>) context
				.getAttribute(SESSION_MAP);
		if (map == null) {
			map = new ConcurrentHashMap<String, HttpSession>();
			context.setAttribute(SESSION_MAP, map);
		}
		return map;
	}

	public static OnlineUser getOnlineUser(HttpServletRequest req) {
		return (OnlineUser) req.getSession().getAttribute(SESSION_USER);
	}

}
